package cn.edu.nuc.ssm.controller;

import cn.edu.nuc.ssm.dto.EverySumCaiWuDto;
import cn.edu.nuc.ssm.dto.SumCaiWuDto;
import cn.edu.nuc.ssm.entity.Account;
import cn.edu.nuc.ssm.entity.User;
import cn.edu.nuc.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/8.
 */
@Controller
@RequestMapping("/front")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/accountlist")
    public String listUserAccount(HttpSession session, Model model){

        User user = (User) session.getAttribute("user");
        if (user==null){
            return "redirect:/front/tologin";
        }
        Map<String, List<Account>> listMap = accountService.listUserAcount(user.getId());
        SumCaiWuDto sumCaiWuDto = accountService.sumCaiWu(user.getId());
        List<EverySumCaiWuDto> everySumCaiWuDtos = accountService.sumEverySumCaiWuDto(user.getId());

        if (everySumCaiWuDtos==null){
            model.addAttribute("msg","数据发生错误");
            return "front/error/error";

        }
        model.addAttribute("everysum",everySumCaiWuDtos);
        if (sumCaiWuDto==null){

            model.addAttribute("msg","数据发生错误");
            return "front/error/error";
        }
        model.addAttribute("sumcaiwu",sumCaiWuDto);
        if (listMap==null||listMap.size()<=0){

            model.addAttribute("msg","数据发生错误");
            return "front/error/error";
        }
        model.addAttribute("acountMapList",listMap);

        return "front/account_index";
    }

    @RequestMapping(value = "/account/update",method = RequestMethod.GET)
    public String updateAccount(Account account,Model model) {

        Account findAccount = accountService.findbyId(account.getId());

        model.addAttribute("account",findAccount);

        return "front/account_update";
    }

    @RequestMapping(value = "/account/update",method = RequestMethod.POST)
    public String updateAccountYuE(Account account,Model model) {

        accountService.updateYuE(account);


        return "redirect:/front/accountlist";
    }
}
